package view;

import model.CodeLine;
import model.CommandParameter;
import model.Constant;
import model.Reference;
import model.Register;
import model.Variable;

public class RegisterViewString extends RegisterView {

	private static final String COLUMN_NAME = "Code";

	@Override
	public String getColumnName() {
		return COLUMN_NAME;
	}

	@Override
	public String convertValue(Register value) {
		return display(value.getCommand());
	}

	private String display(CodeLine command) {
		if (command instanceof Constant) {
			return String.valueOf(((Constant) command).getValue());
		}
		StringBuffer s = new StringBuffer(command.getName()).append(" ");
		for (CommandParameter parameter : command.getCommandParameter()) {
			if (parameter instanceof Reference) {
				s.append(((Reference) parameter).getAddress()).append(", ");

			} else {
				s.append(((Variable) parameter).getName()).append(", ");
			}
		}
		if (s.substring(s.length() - 2).equals(", ")) {
			return s.substring(0, s.length() - 2);
		} else {
			return s.substring(0, s.length() - 1);
		}
	}
}
